import { IEchartsData } from '@/model/echarts/echartsInter'
import { dataFlag, noSeries, sortObj } from '@/utils/echartsCommonFun'
import {
  EChartsOption,
  PieSeriesOption
} from 'echarts/types/dist/shared'

export function Chart (datas:IEchartsData) {
  const series = []
  const flag = dataFlag(datas.data)
  if (flag > 0 && flag === datas.data.length) { // 没有数据
    series.push(noSeries())
  } else {
    datas.data.forEach(data => {
      let value:Array<{name:string, value:number}> = []
      if (data.value) {
        value = data.value
      }
      value = sortObj(value)
      series.push({
        name: data.name ? data.name : '',
        type: 'pie',
        roseType: 'radius',
        radius: '80%',
        center: ['50%', '50%'],
        label: {
          fontSize: '12',
          color: '#fff',
          formatter: '{d}' + '%'
        },
        labelLine: {
          show: true,
          length: 0
        },
        selectedOffset: 5,
        data: value
      } as PieSeriesOption)
    })
  }
  return {
    color: ['#E4F3DB', '#009A9F', '#CD163F', '#00AEEF', '#77C14C', '#393939'],
    series: series
  } as EChartsOption
}
